package com.jason.sms.agent.protocol.cmpp3.component;

import java.util.LinkedList;

import com.jason.sms.agent.protocol.cmpp3.handler.Handler;
import com.jason.sms.agent.protocol.cmpp3.msg.Msg;

public class SmsSendedManager {
	
	private LinkedList<Msg> msgList = new LinkedList<Msg>();
	
	private Byte lock = new Byte((byte)0x01);
	
	private Handler handler;
	
	private static SmsSendedManager manager;
	
	
	
	private SmsSendedManager(){
		
	}
	
	public static SmsSendedManager getManager(){
		if(manager == null){
			return new SmsSendedManager();
		}else{
			return manager;
		}
	}
	
	public void setHandler(Handler handler){
		this.handler = handler;
	}
	
	public void addSendMsg(Msg msg){
		synchronized(lock){
			msgList.addLast(msg);
		}
	}
	
	public void getRecvMsg(Msg msg){
		synchronized(lock){
			Msg respMsg = null;
			for(Msg mg:msgList){
				if(mg.getSequenceId() == msg.getSequenceId()){
					respMsg = mg;
					msgList.remove(mg);					
					break;
				}
			}
			if(respMsg != null){
				handler.handle(respMsg);
			}
		}
		
	}
	
	
	
	
	

}
